

package com.example.administrator.caigou51.recyclerCard.cardView;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.CheckOrderActivity;
import com.example.administrator.caigou51.activity.GBaseActivity;
import com.example.administrator.caigou51.activity.OrderListActivity;
import com.example.administrator.caigou51.activity.PayMoneyActivity;
import com.example.administrator.caigou51.activity.WebHtmlActivity;
import com.example.administrator.caigou51.bean.OrderDetailBean;
import com.example.administrator.caigou51.bean.OrderInfoBean;
import com.example.administrator.caigou51.bean.PayBean;
import com.example.administrator.caigou51.global.Constant;
import com.example.administrator.caigou51.recyclerCard.basic.CardItemView;
import com.example.administrator.caigou51.recyclerCard.card.OrderDetail3Card;
import com.example.administrator.caigou51.util.ToastUtil;
import com.example.administrator.caigou51.widget.dialog.SweetDialogManager;

import java.io.Serializable;

import cn.pedant.SweetAlert.SweetAlertDialog;


public class OrderDetail3CardView extends CardItemView<OrderDetail3Card> {
 	private Context mContext;
    private TextView textViewPayType;
    private TextView textViewGoodsJinE;
    private TextView textViewYouHuiJinE;
    private TextView textViewDingDan;
    private TextView textViewOrderTime;
    private RelativeLayout linearLayout5;
    private TextView textViewPayMoneyRed,textViewPeiSongYunFei;
    private TextView textViewBuyAgainGrey;
    OrderDetail3Card card;
    public OrderDetail3CardView(Context context) {
        super(context);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public OrderDetail3CardView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        // TODO Auto-generated constructor stub
    }

    public OrderDetail3CardView(Context context,
                                    AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mContext=context;
        // TODO Auto-generated constructor stub
    }
    @Override
    public void build(final OrderDetail3Card card) {
        // TODO Auto-generated method stub
        super.build(card);
        this.card=card;

        textViewPayType = (TextView) findViewById(R.id.textViewPayType);
        textViewGoodsJinE = (TextView) findViewById(R.id.textViewGoodsJinE);
        textViewYouHuiJinE = (TextView) findViewById(R.id.textViewYouHuiJinE);
        textViewDingDan = (TextView) findViewById(R.id.textViewDingDan);
        textViewOrderTime = (TextView) findViewById(R.id.textViewOrderTime);
        linearLayout5 = (RelativeLayout) findViewById(R.id.linearLayout5);
        textViewPayMoneyRed = (TextView) findViewById(R.id.textViewPayMoneyRed);
        textViewBuyAgainGrey = (TextView) findViewById(R.id.textViewBuyAgainGrey);
        textViewPeiSongYunFei = (TextView) findViewById(R.id.textViewPeiSongYunFei);

        textViewPeiSongYunFei.setText(card.getOrderDetailBean().getShipping_fee());
        textViewPayType.setText(card.getOrderDetailBean().getPay_name()+"");
        textViewGoodsJinE.setText(card.getOrderDetailBean().getTotal_amount()+"");
        textViewYouHuiJinE.setText(card.getOrderDetailBean().getDiscount_amount());
        textViewDingDan.setText(card.getOrderDetailBean().getPay_amount()+"");
        textViewOrderTime.setText("下单时间:  "+card.getOrderDetailBean().getAddtime());

        if (card.getOrderDetailBean().getStatus().equals(OrderListActivity.DFK+""))
        {
            textViewPayMoneyRed.setVisibility(View.VISIBLE);
            textViewPayMoneyRed.setBackgroundResource(R.drawable.shape_radian_red);
            textViewPayMoneyRed.setText("去支付");
            textViewPayMoneyRed.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    PayBean payBean = new PayBean();
                    payBean.setOrder_id(card.getOrderDetailBean().getOrder_id());
                    payBean.setPay_amount(card.getOrderDetailBean().getPay_amount()+"");
                    Intent intetn =new Intent(mContext,PayMoneyActivity.class);
                    intetn.putExtra(PayMoneyActivity.TagPayBean, (Serializable) payBean);
                    mContext.startActivity(intetn);
                }
            });
            textViewBuyAgainGrey.setVisibility(View.VISIBLE);
            textViewBuyAgainGrey.setText("取消订单");
            textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity) mContext, "确认要取消订单吗?", "", false, false, new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();

                        }
                    }, new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismiss();
                            OrderListItemCardView.taskCancelOrder(false,(GBaseActivity) mContext,card.getOrderDetailBean().getOrder_id());
                        }
                    });
                }
            });
        }else if (card.getOrderDetailBean().getStatus().equals(OrderListActivity.DFH+""))
        {
            textViewPayMoneyRed.setVisibility(View.GONE);
            textViewBuyAgainGrey.setVisibility(View.VISIBLE);
            textViewBuyAgainGrey.setText("取消订单");
            textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity) mContext, "确认要取消订单吗?", "", false, false, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismiss();

                                }
                            }, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismiss();
                                    OrderListItemCardView.taskCancelOrder(false,(GBaseActivity) mContext,card.getOrderDetailBean().getOrder_id());
                                }
                            });
                        }
                    });
                }
            });
        }else if (card.getOrderDetailBean().getStatus().equals(OrderListActivity.DSH+""))
        {
            textViewPayMoneyRed.setVisibility(View.VISIBLE);
            textViewPayMoneyRed.setBackgroundResource(R.drawable.shape_radian_green);
            textViewPayMoneyRed.setText("查看物流");
            textViewPayMoneyRed.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =new Intent(mContext, WebHtmlActivity.class);
                    intent.putExtra(WebHtmlActivity.Title,"物流详情");
                    intent.putExtra(WebHtmlActivity.WebUrl, Constant.getHtml5UrlFotParameter(Constant.HTML5.Html5_check_order_state,card.getOrderDetailBean().getExpress_type(),card.getOrderDetailBean().getExpress_number()));
                    mContext.startActivity(intent);
                }
            });
            textViewBuyAgainGrey.setVisibility(View.VISIBLE);
            textViewBuyAgainGrey.setText("咨询药师");
            textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((GBaseActivity)mContext).GoToChat();
                }
            });
        }else if (card.getOrderDetailBean().getStatus().equals(OrderListActivity.JYWG+""))
        {
            textViewPayMoneyRed.setVisibility(View.VISIBLE);
            textViewPayMoneyRed.setBackgroundResource(R.drawable.shape_radian_green);
            textViewPayMoneyRed.setText("再次购买");
            textViewPayMoneyRed.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =new Intent(mContext, CheckOrderActivity.class);
                    intent.putExtra(CheckOrderActivity.TagItemIDS,getCheckIDS());
                    intent.putExtra(CheckOrderActivity.TagNumber,getCheckNumberS());
                    intent.putExtra(CheckOrderActivity.TagType,CheckOrderActivity.TagTypeBugAgain);
                    mContext.startActivity(intent);
                }
            });
            textViewBuyAgainGrey.setVisibility(View.VISIBLE);
            textViewBuyAgainGrey.setText("咨询药师");
            textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((GBaseActivity)mContext).GoToChat();
                }
            });
        }else if (card.getOrderDetailBean().getStatus().equals(OrderListActivity.YQX_1+"") || card.getOrderDetailBean().getStatus().equals(OrderListActivity.YQX_2+""))
        {
            textViewPayMoneyRed.setVisibility(View.VISIBLE);
            textViewPayMoneyRed.setBackgroundResource(R.drawable.shape_radian_green);
            textViewPayMoneyRed.setText("再次购买");
            textViewBuyAgainGrey.setVisibility(View.VISIBLE);
            textViewBuyAgainGrey.setText("咨询药师");
            textViewPayMoneyRed.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent =new Intent(mContext, CheckOrderActivity.class);
                    intent.putExtra(CheckOrderActivity.TagItemIDS,getCheckIDS());
                    intent.putExtra(CheckOrderActivity.TagNumber,getCheckNumberS());
                    intent.putExtra(CheckOrderActivity.TagType,CheckOrderActivity.TagTypeBugAgain);
                    mContext.startActivity(intent);
                }
            });
            textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((GBaseActivity)mContext).GoToChat();
                }
            });
        }else if (card.getOrderDetailBean().getStatus().equals(OrderListActivity.HDFK+"") )
        {
            textViewPayMoneyRed.setVisibility(View.GONE);
            textViewBuyAgainGrey.setVisibility(View.VISIBLE);
            textViewBuyAgainGrey.setText("取消订单");
            textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    textViewBuyAgainGrey.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            SweetDialogManager.getInstance().showDialogByWarmTitleText((GBaseActivity) mContext, "确认要取消订单吗?", "", false, false, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismiss();

                                }
                            }, new SweetAlertDialog.OnSweetClickListener() {
                                @Override
                                public void onClick(SweetAlertDialog sweetAlertDialog) {
                                    sweetAlertDialog.dismiss();
                                    OrderListItemCardView.taskCancelOrder(false,(GBaseActivity) mContext,card.getOrderDetailBean().getOrder_id());
                                }
                            });
                        }
                    });
                }
            });
        }
    }

    private String getCheckIDS() {
        String ids="";
        for (int i=0;i<card.getOrderDetailBean().getGoods().size();i++)
        {
            OrderDetailBean.GoodsEntity goodsEntity=card.getOrderDetailBean().getGoods().get(i);

            ids+=goodsEntity.getItemid()+",";

        }
        if (!TextUtils.isEmpty(ids)) {
            ids = ids.substring(0, ids.length() - 1);
        }
        return ids;
    }

    private String getCheckNumberS() {
        String numbers="";
        for (int i=0;i<card.getOrderDetailBean().getGoods().size();i++)
        {
            OrderDetailBean.GoodsEntity goodsEntity=card.getOrderDetailBean().getGoods().get(i);

            numbers+=goodsEntity.getNumber()+",";

        }
        if (!TextUtils.isEmpty(numbers)) {
            numbers = numbers.substring(0, numbers.length() - 1);
        }
        return numbers;
    }
}
